<?php

class content_mdl_article_bodys extends dbeav_model {

    public function format_params($params) {
        if (isset($params['seo_title']))
            $params['seo_title'] = htmlspecialchars($params['seo_title'], ENT_QUOTES);
        if (isset($params['seo_keywords']))
            $params['seo_keywords'] = htmlspecialchars($params['seo_keywords'], ENT_QUOTES);
        if (isset($params['seo_description']))
            $params['seo_description'] = htmlspecialchars($params['seo_description'], ENT_QUOTES);
        if (isset($params['content']))
            $params['content'] = $params['content'];
        $params['length'] = strlen($params['content']);
        return $params;
    }
 // End Function
    public function valid_insert($params) {
        if (empty($params['article_id'])) {
            trigger_error(app::get('content')->_('文章ID不能为空'), E_USER_ERROR);
            return false;
        }
        $params = $this->format_params($params);
        return $params;
    }
 // End Function
    public function valid_update($params) {
        $params = $this->format_params($params);
        return $params;
    }
 // End Function
    public function insert(&$params) {
        $params = $this->valid_insert($params);
        if (! $params)
            return false;
        $insert_id = parent::insert($params);
        if ($insert_id) {
            $rows = $this->getList('article_id', array(
                'id' => $insert_id
            ));
            kernel::single('content_article_detail')->delete_body_kvstore($rows[0]['article_id']);
            kernel::single('content_article_detail')->store_detail_change();
            return $insert_id;
        } else {
            return false;
        }
    }
 // End Function
    public function update($params, $filter = array(), $mustUpdate = null) {
        $params = $this->valid_update($params);
        if (! $params)
            return false;
        $rows = $this->getList('article_id', $filter);
        if (parent::update($params, $filter)) {
            foreach ($rows as $row) {
                kernel::single('content_article_detail')->delete_body_kvstore($row['article_id']);
            }
            kernel::single('content_article_detail')->store_detail_change();
            return true;
        } else {
            return false;
        }
    }
 // End Function
    public function delete($filter, $subSdf = 'delete') {
        $rows = $this->getList('article_id', $filter);
        if (parent::delete($filter)) {
            foreach ($rows as $row) {
                kernel::single('content_article_detail')->delete_body_kvstore($row['article_id']);
            }
            kernel::single('content_article_detail')->store_detail_change();
            return true;
        } else {
            return false;
        }
    } // End Function
}//End Class
